#include "stdafx.h"
#include "../System/NDSFile.h"
#include "../System/CommonString.h"
#include "StringManager.h"
#include "Sprite_IDs.h"
#include <string.h>
#include "../Game.h"
#include "../font_KR_char_map.h"
#include "../font_SR_char_map.h"
#include "../font_JP_char_map.h"

#include "../font_KR_shrek_char_map.h"
#include "../font_SR_shrek_char_map.h"
#include "../font_JP_shrek_char_map.h"

#include "../font_KR_shrek_black_char_map.h"
#include "../font_SR_shrek_black_char_map.h"
#include "../font_JP_shrek_black_char_map.h"

#include "GameMenu_Define.h"

////////////////////////////////////////////////////////////////////////////////////////////////////
//SPRITE_KR_FONT

////////////////////////////////////////////////////////////////////////////////////////////////////

#define KFile_Strings_EN		"./strings/EN.bar"
#define KFile_Strings_DE		"./strings/DE.bar"
#define KFile_Strings_FR		"./strings/FR.bar"
#define KFile_Strings_IT		"./strings/IT.bar"
#define KFile_Strings_SP		"./strings/ES.bar"
#define KFile_Strings_KR		"./strings/KO.bar"
#define KFile_Strings_JP		"./strings/JA.bar"
#define KFile_Strings_SR		"./strings/ZH.bar"


////////////////////////////////////////////////////////////////////////////////////////////////////

void StringManager::LoadAllFont()
{
	int i = 0;
	for(i = 0; i <NUM_OF_FONTS; i++)
	{
		LoadFont(i);
	}
}

void StringManager::UnLoadFont(int id)
{
	switch(id)
	{
	case SPRITE_INTERFACE_MENU_FONT:
		m_fonts[FONT_MENU] = NULL;			
		break;
	case SPRITE_INTERFACE_FONT_BROWN_GAME:
		m_fonts[FONT_BROWN_GAME] = NULL;
		break;	
	case SPRITE_INTERFACE_FONT_WHITE_GAME:
		m_fonts[FONT_WHITE_GAME] = NULL;	
		break;	
	case SPRITE_INTERFACE_FONT_BLACK:
		m_fonts[FONT_BLACK] = NULL;		
		break;	
	case SPRITE_INTERFACE_FONT_GREEN_GAME:
		m_fonts[FONT_GREEN_GAME] = NULL;			
		break;	
	case SPRITE_INTERFACE_FONT_YELLOW:
		m_fonts[FONT_YELLOW] = NULL;			
		break;	
	case SPRITE_INTERFACE_FONT_SHINE:
		m_fonts[FONT_SHINE] = NULL;		
		break;	
	case SPRITE_INTERFACE_FONT_BLACK_SMALL:
		m_fonts[FONT_BLACK_SMALL] = NULL;		
		break;
	case SPRITE_INTERFACE_FONT_BLACK_SMALL_SHINE:
		m_fonts[FONT_BLACK_SMALL_SHINE] = NULL;
		break;
	case SPRITE_INTERFACE_FONT_LOAD:
		m_fonts[FONT_LOAD] = NULL;
		break;
	case SPRITE_FONT_SHREK_RESULT:
		m_fonts[FONT_SHREK_RESULT] = NULL;
		break;
	case SPRITE_FONT_RED:
		m_fonts[FONT_RED] = NULL;
		break;
	}
}

void StringManager::LoadFont(const int fontID)
{
	if(LANGUAGE_KR == m_nLanguage)
	{
		switch(fontID)
		{
		case FONT_MENU:
			CreateAndSetKRFont(fontID, SPRITE_KR_FONT_SHREK);
			break;
		case FONT_BROWN_GAME:
			CreateAndSetKRFont(fontID, SPRITE_KR_FONT_SHREK_LOADING);
			break;
		case FONT_WHITE_GAME:
			CreateAndSetKRFont(fontID, SPRITE_KR_FONT_SHREK_LOADING);
			break;
		case FONT_BLACK:
			CreateAndSetKRFont(fontID, SPRITE_KR_FONT_SHREK_BLACK);
			break;
		case FONT_GREEN_GAME:
			CreateAndSetKRFont(fontID, SPRITE_KR_FONT_SHREK_LOADING);
			break;
		case FONT_YELLOW:
			CreateAndSetKRFont(fontID, SPRITE_KR_FONT_SHREK_LOADING);
			break;
		case FONT_RED:
			CreateAndSetKRFont(fontID, SPRITE_KR_FONT_SHREK_LOADING);
			break;
		case FONT_BLACK_SMALL:
			CreateAndSetKRFont(fontID, SPRITE_KR_FONT_SHREK_BLACK);
			break;
		case FONT_BLACK_SMALL_SHINE:
			CreateAndSetKRFont(fontID, SPRITE_KR_FONT_SHREK_LOADING);
			break;
		case FONT_LOAD:
			CreateAndSetKRFont(fontID, SPRITE_KR_FONT_SHREK_LOADING);
			break;
		case FONT_SHREK_RESULT:
			CreateAndSetKRFont(fontID, SPRITE_KR_FONT_SHREK_LOADING);
			break;
		}
		m_fonts[fontID]->SetLineSpacing(ASIAN_FONT_DEFAULT_LINE_SPACE);
	}
	else if(LANGUAGE_JP == m_nLanguage)
	{
		switch(fontID)
		{
		case FONT_MENU:
			CreateAndSetJPFont(fontID, SPRITE_JP_FONT_SHREK);
			break;
		case FONT_BROWN_GAME:
			CreateAndSetJPFont(fontID, SPRITE_JP_FONT_SHREK_LOADING);
			break;
		case FONT_WHITE_GAME:
			CreateAndSetJPFont(fontID, SPRITE_JP_FONT_SHREK_LOADING);
			break;
		case FONT_BLACK:
			CreateAndSetJPFont(fontID, SPRITE_JP_FONT_SHREK_BLACK);
			break;
		case FONT_GREEN_GAME:
			CreateAndSetJPFont(fontID, SPRITE_JP_FONT_SHREK_LOADING);
			break;
		case FONT_YELLOW:
			CreateAndSetJPFont(fontID, SPRITE_JP_FONT_SHREK_LOADING);
			break;
		case FONT_RED:	
			CreateAndSetJPFont(fontID, SPRITE_JP_FONT_SHREK_LOADING);
			break;
		case FONT_BLACK_SMALL:
			CreateAndSetJPFont(fontID, SPRITE_JP_FONT_SHREK_BLACK);
			break;
		case FONT_BLACK_SMALL_SHINE:
			CreateAndSetJPFont(fontID, SPRITE_JP_FONT_SHREK_LOADING);
			break;
		case FONT_LOAD:
			CreateAndSetJPFont(fontID, SPRITE_JP_FONT_SHREK_LOADING);
			break;
		case FONT_SHREK_RESULT:
			CreateAndSetJPFont(fontID, SPRITE_JP_FONT_SHREK_LOADING);
			break;
		}
		m_fonts[fontID]->SetLineSpacing(2);
	}
	else if(LANGUAGE_SR == m_nLanguage)
	{
		switch(fontID)
		{
		case FONT_MENU:
			CreateAndSetSRFont(fontID, SPRITE_SR_FONT_SHREK);
			break;
		case FONT_BROWN_GAME:
			CreateAndSetSRFont(fontID, SPRITE_SR_FONT_SHREK_LOADING);
			break;
		case FONT_WHITE_GAME:
			CreateAndSetSRFont(fontID, SPRITE_SR_FONT_SHREK_LOADING);
			break;
		case FONT_BLACK:
			CreateAndSetSRFont(fontID, SPRITE_SR_FONT_SHREK_BLACK);
			break;
		case FONT_GREEN_GAME:
			CreateAndSetSRFont(fontID, SPRITE_SR_FONT_SHREK_LOADING);
			break;
		case FONT_YELLOW:
			CreateAndSetSRFont(fontID, SPRITE_SR_FONT_SHREK_LOADING);
			break;
		case FONT_RED:	
			CreateAndSetSRFont(fontID, SPRITE_SR_FONT_SHREK_LOADING);
			break;
		case FONT_BLACK_SMALL:
			CreateAndSetSRFont(fontID, SPRITE_SR_FONT_SHREK_BLACK);
			break;
		case FONT_BLACK_SMALL_SHINE:
			CreateAndSetSRFont(fontID, SPRITE_SR_FONT_SHREK_LOADING);
			break;
		case FONT_LOAD:
			CreateAndSetSRFont(fontID, SPRITE_SR_FONT_SHREK_LOADING);
			break;
		case FONT_SHREK_RESULT:
			CreateAndSetSRFont(fontID, SPRITE_SR_FONT_SHREK_LOADING);
			break;
		}
		m_fonts[fontID]->SetLineSpacing(0);
	}
	else
	{
		switch(fontID)
		{
		case FONT_MENU:
			m_fonts[fontID] = CGame::GetGame()->GetSprite(SPRITE_INTERFACE_MENU_FONT);
			m_nFontSpriteID[fontID] = SPRITE_INTERFACE_MENU_FONT;
			m_fonts[fontID]->LoadCharacterMap();
			break;
		case FONT_BROWN_GAME:
			m_fonts[fontID] = CGame::GetGame()->GetSprite(SPRITE_INTERFACE_FONT_BROWN_GAME);
			m_fonts[fontID]->NearestImage();
			m_nFontSpriteID[fontID] = SPRITE_INTERFACE_FONT_BROWN_GAME;
			m_fonts[fontID]->LoadCharacterMap();
			break;	
		case FONT_WHITE_GAME:
			m_fonts[fontID] = CGame::GetGame()->GetSprite(SPRITE_INTERFACE_FONT_WHITE_GAME);
			m_fonts[fontID]->NearestImage();
			m_nFontSpriteID[fontID] = SPRITE_INTERFACE_FONT_WHITE_GAME;
			m_fonts[fontID]->LoadCharacterMap();	
			break;	
		case FONT_BLACK:
			m_fonts[fontID] = CGame::GetGame()->GetSprite(SPRITE_INTERFACE_FONT_BLACK);
			m_nFontSpriteID[fontID] = SPRITE_INTERFACE_FONT_BLACK;
			m_fonts[fontID]->LoadCharacterMap();		
			break;	
		case FONT_GREEN_GAME:
			m_fonts[fontID] = CGame::GetGame()->GetSprite(SPRITE_INTERFACE_FONT_GREEN_GAME);
			m_fonts[fontID]->NearestImage();
			m_nFontSpriteID[fontID] = SPRITE_INTERFACE_FONT_GREEN_GAME;
			m_fonts[fontID]->LoadCharacterMap();			
			break;	
		case FONT_YELLOW:
			m_fonts[fontID] = CGame::GetGame()->GetSprite(SPRITE_INTERFACE_FONT_YELLOW);
			m_nFontSpriteID[fontID] = SPRITE_INTERFACE_FONT_YELLOW;
			m_fonts[fontID]->LoadCharacterMap();			
			break;	
		case FONT_BLACK_SMALL:
			m_fonts[fontID] = CGame::GetGame()->GetSprite(SPRITE_INTERFACE_FONT_BLACK_SMALL);
			m_nFontSpriteID[fontID] = SPRITE_INTERFACE_FONT_BLACK_SMALL;
			m_fonts[fontID]->LoadCharacterMap();	
			m_fonts[fontID]->SetLineSpacing(6);
			break;
		case FONT_BLACK_SMALL_SHINE:
			m_fonts[fontID] = CGame::GetGame()->GetSprite(SPRITE_INTERFACE_FONT_BLACK_SMALL_SHINE);
			m_nFontSpriteID[fontID] = SPRITE_INTERFACE_FONT_BLACK_SMALL_SHINE;
			m_fonts[fontID]->LoadCharacterMap();
			break;
		case FONT_LOAD:
			m_fonts[fontID] = CGame::GetGame()->GetSprite(SPRITE_INTERFACE_FONT_LOAD);
			m_fonts[fontID]->NearestImage();
			m_nFontSpriteID[fontID] = SPRITE_INTERFACE_FONT_LOAD;
			m_fonts[fontID]->LoadCharacterMap();
			break;
		case FONT_SHREK_RESULT:
			m_fonts[fontID] = CGame::GetGame()->GetSprite(SPRITE_FONT_SHREK_RESULT);
			m_nFontSpriteID[fontID] = SPRITE_FONT_SHREK_RESULT;
			m_fonts[fontID]->LoadCharacterMap();
			break;
		case FONT_RED:
			m_fonts[fontID] = CGame::GetGame()->GetSprite(SPRITE_FONT_RED);
			m_nFontSpriteID[fontID] = SPRITE_FONT_RED;
			m_fonts[fontID]->LoadCharacterMap();
			break;
		}
		if(LANGUAGE_FR == m_nLanguage)
		{
			m_fonts[fontID]->SetIsFRFont(true);
		}
		else
		{
			m_fonts[fontID]->SetIsFRFont(false);
		}
	}
}

void StringManager::CreateAndSetKRFont(int fontID, int spriteID)
{
	m_fonts[fontID] = CGame::GetGame()->GetSprite(spriteID);
	m_nFontSpriteID[fontID] = spriteID;
	m_fonts[fontID]->FreeMapChar(LANGUAGE_KR);
	unsigned short * charMap = NULL;
	if(FONT_MENU == fontID)
	{
		charMap = (unsigned short *)KR_font_shrek_chr_map;
		m_fonts[fontID]->SetMapCharCount(sizeof(KR_font_shrek_chr_map) / sizeof(KR_font_shrek_chr_map[0]));
	}
	else if(FONT_BLACK == fontID || FONT_BLACK_SMALL == fontID)
	{
		charMap = (unsigned short *)KR_font_shrek_black_chr_map;
		m_fonts[fontID]->SetMapCharCount(sizeof(KR_font_shrek_black_chr_map) / sizeof(KR_font_shrek_black_chr_map[0]));
	}
	else
	{
		charMap = (unsigned short *)KR_font_shrek_loading_chr_map;
		m_fonts[fontID]->SetMapCharCount(sizeof(KR_font_shrek_loading_chr_map) / sizeof(KR_font_shrek_loading_chr_map[0]));
	}
	m_fonts[fontID]->SetCharacterMap(charMap);
	m_fonts[fontID]->SetIsKRFont(true);
}

void StringManager::CreateAndSetJPFont(int fontID, int spriteID)
{
	m_fonts[fontID] = CGame::GetGame()->GetSprite(spriteID);
	m_nFontSpriteID[fontID] = spriteID;
	m_fonts[fontID]->FreeMapChar(LANGUAGE_JP);
	unsigned short * charMap = NULL;
	if(FONT_MENU == fontID)
	{
		charMap = (unsigned short *)JP_font_shrek_chr_map;
		m_fonts[fontID]->SetMapCharCount(sizeof(JP_font_shrek_chr_map) / sizeof(JP_font_shrek_chr_map[0]));
	}
	else if(FONT_BLACK == fontID || FONT_BLACK_SMALL == fontID)
	{
		charMap = (unsigned short *)JP_font_shrek_black_chr_map;
		m_fonts[fontID]->SetMapCharCount(sizeof(JP_font_shrek_black_chr_map) / sizeof(JP_font_shrek_black_chr_map[0]));
	}
	else
	{
		charMap = (unsigned short *)JP_font_shrek_loading_chr_map;
		m_fonts[fontID]->SetMapCharCount(sizeof(JP_font_shrek_loading_chr_map) / sizeof(JP_font_shrek_loading_chr_map[0]));
	}
	m_fonts[fontID]->SetCharacterMap(charMap);
	m_fonts[fontID]->SetIsJPFont(true);
}

void StringManager::CreateAndSetSRFont(int fontID, int spriteID)
{
	m_fonts[fontID] = CGame::GetGame()->GetSprite(spriteID);
	m_nFontSpriteID[fontID] = spriteID;
	m_fonts[fontID]->FreeMapChar(LANGUAGE_SR);
	unsigned short * charMap = NULL;
	if(FONT_MENU == fontID)
	{
		charMap = (unsigned short *)SR_font_shrek_chr_map;
		m_fonts[fontID]->SetMapCharCount(sizeof(SR_font_shrek_chr_map) / sizeof(SR_font_shrek_chr_map[0]));
	}
	else if(FONT_BLACK == fontID || FONT_BLACK_SMALL == fontID)
	{
		charMap = (unsigned short *)SR_font_shrek_black_chr_map;
		m_fonts[fontID]->SetMapCharCount(sizeof(SR_font_shrek_black_chr_map) / sizeof(SR_font_shrek_black_chr_map[0]));
	}
	else
	{
		charMap = (unsigned short *)SR_font_shrek_loading_chr_map;
		m_fonts[fontID]->SetMapCharCount(sizeof(SR_font_shrek_loading_chr_map) / sizeof(SR_font_shrek_loading_chr_map[0]));
	}
	m_fonts[fontID]->SetCharacterMap(charMap);
	m_fonts[fontID]->SetIsSRFont(true);
}

////////////////////////////////////////////////////////////////////////////////////////////////////

StringPack::StringPack()
{
	m_nStrings	=	0;
	m_pOffsets	=	NULL;
	m_pData		=	NULL;
}

////////////////////////////////////////////////////////////////////////////////////////////////////

StringPack::~StringPack()
{
	m_nStrings	=	0;
	Unload();	
}

////////////////////////////////////////////////////////////////////////////////////////////////////
//Loads a string pack from the given file

int StringPack::Load(CNDSFile * pFile, int dataSize, int flags)
{
	//Read number of strings
	m_nStrings	=	pFile->ReadShort();
	
	//Perform a skip if the pack is already loaded
	if(	(flags & PACK_LOAD_FULL) &&
		(m_pData))
		flags	=	PACK_LOAD_SKIP;

	//Read / skip
	if(flags == PACK_LOAD_FULL)
	{
		//Offsets
		if(m_nStrings)
		{
			m_pOffsets	=	NEW uint16[m_nStrings];
			if(!m_pOffsets)
				return -1;

			pFile->Read((byte*)m_pOffsets, sizeof(uint16) * m_nStrings);
			dataSize -= 2 + sizeof(uint16) * m_nStrings;

			//Data
			if(dataSize)
			{
				// 2 bytes per character
				dataSize = dataSize >> 1;
				m_pData	=	NEW TChar[dataSize];
				if(!m_pData)
					return -2;
				//pFile->read(m_pData, sizeof(byte) * dataSize); 
				for(int i = 0; i < dataSize; i++)
					//m_pData[i] = pFile->ReadShort();
                    m_pData[i] = pFile->ReadUnsignedShort();
			}
		}
	}
	else
	{	
		pFile->Seek(CNDSFile::SEEKMODE_CUR,dataSize - 2);
	}

	//No error
	return 0;
}

////////////////////////////////////////////////////////////////////////////////////////////////////

void StringPack::Unload()
{
	SAFE_DELETE_ARRAY(m_pOffsets);
	SAFE_DELETE_ARRAY(m_pData);
}

////////////////////////////////////////////////////////////////////////////////////////////////////

TChar* StringPack::GetString(int id)
{
	if(!m_pData)
		return null;
	//characters are exported in 2 bytes
	return (TChar*)(m_pData + (m_pOffsets[id]>>1));
}

////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////

StringManager::StringManager()
{
	m_nLanguage		=	LANGUAGE_EN;
	m_pPackLoaded = 0;

	m_nCurrentlyEscapeDigit = 0;

	memset(m_nFontSpriteID, -1, sizeof(int) * NUM_OF_FONTS);
}

////////////////////////////////////////////////////////////////////////////////////////////////////

StringManager::~StringManager()
{
	FreeAllFontRes();
	for(int i = 0; i < LANGUAGE_NUM; ++ i)
		for(int j = 0; j < NUM_PACKS; ++j)
			m_pPacks[i][j].Unload();
}

void StringManager::FreeAllFontRes()
{
	for (int i = 0; i < NUM_OF_FONTS; i++)
	{
		if(m_nFontSpriteID[i] != -1)
		{
			CGame::GetGame()->UnLoadSprite(m_nFontSpriteID[i]);
			m_nFontSpriteID[i] = -1;
			m_fonts[i] = NULL;
		}
	}
}

void StringManager::FreeAllPack()
{
	for(int i = 0 ; i < NUM_PACKS ; i++)
	{
		m_pPackLoaded	&=	~(1 << i);
	}
}

////////////////////////////////////////////////////////////////////////////////////////////////////
//Loads / frees unused string packs

int StringManager::RefreshPacks(int flags)
{
	//Free old font res.
	FreeAllFontRes();

	//No error
	return 0;
}

void StringManager::LoadAllString()
{
	//Open strings file
	char*	files[LANGUAGE_NUM] = {	KFile_Strings_EN, KFile_Strings_FR, 
							KFile_Strings_DE, KFile_Strings_IT,
							KFile_Strings_SP, KFile_Strings_KR,
							KFile_Strings_JP, KFile_Strings_SR
						};

	CNDSFile pFile;

	for(int j = 0; j < LANGUAGE_NUM; ++j)
	{
		BOOL bRet = pFile.Open(files[j]);
		if(!bRet)
			ASSERT(false);

		//Sanity check
		int	nFiles = pFile.ReadShort();

		//File offsets
		uint32*	pOffsets = NEW uint32[nFiles + 1];
		if(!pOffsets)
			ASSERT(false);
		pFile.Read((byte*)pOffsets, sizeof(uint32) * (nFiles + 1));

		//At the beginning of the first pack
		for(int i = 0; i < NUM_PACKS; i++)
		{
			int dataSize = pOffsets[i+ 1] - pOffsets[i];

			//Note that if the pack is already loaded, it will skip file data.
			m_pPacks[j][i].Load(&pFile, dataSize, PACK_LOAD_FULL);
		}

		//Delete temporary buffers, close file
		SafeDeleteArray<uint32>(pOffsets);
		pFile.Close();
	}
}

void StringManager::LoadString(int nLanguage)
{
	//Open strings file
	char*	files[LANGUAGE_NUM] = {	KFile_Strings_EN, KFile_Strings_FR, 
							KFile_Strings_DE, KFile_Strings_IT,
							KFile_Strings_SP, KFile_Strings_KR,
							KFile_Strings_JP, KFile_Strings_SR
						};

	CNDSFile pFile;

	BOOL bRet = pFile.Open(files[nLanguage]);
	if(!bRet)
		ASSERT(false);

	//Sanity check
	int	nFiles = pFile.ReadShort();

	//File offsets
	uint32*	pOffsets = NEW uint32[nFiles + 1];
	if(!pOffsets)
		ASSERT(false);
	pFile.Read((byte*)pOffsets, sizeof(uint32) * (nFiles + 1));

	//At the beginning of the first pack
	for(int i = 0; i < NUM_PACKS; i++)
	{
		int dataSize = pOffsets[i+ 1] - pOffsets[i];

		//Note that if the pack is already loaded, it will skip file data.
		m_pPacks[nLanguage][i].Load(&pFile, dataSize, PACK_LOAD_FULL);
	}

	//Delete temporary buffers, close file
	SafeDeleteArray<uint32>(pOffsets);
	pFile.Close();
}

////////////////////////////////////////////////////////////////////////////////////////////////////
TChar* StringManager::GetString(int id)
{
	if(id == 0xFFFF)	//Special case for -1
		return NULL;

	int	packID	=	id >> 10;
	int strId	=	id - (packID << 10);

	if(packID >= NUM_PACKS)
		return NULL;	//Should never get here

	//Locate pack
	return m_pPacks[m_nLanguage][packID].GetString(strId);

}

////////////////////////////////////////////////////////////////////////////////////////////////////

//bool StringManager::TestString(int id)
//{
//	if(id == 0xFFFF)	//Special case for -1
//		return NULL;
//
//	int	packID	=	id >> 10;
//	int strId	=	id - (packID << 10);
//
//	if(packID >= NUM_PACKS)
//		return false;	//Should never get here
//
//	//Locate pack
//	StringPack	*	pPack	=	m_pPacks + packID;
//	return (strId < pPack->GetNumStrings());
//	
//}

////////////////////////////////////////////////////////////////////////////////////////////////////

//bool TestString(int id)
//{
//	return false;
//	//return IApplication::GetInstance()->GetStringManager()->TestString(id);
//}

////////////////////////////////////////////////////////////////////////////////////////////////////
void StringManager::SetFontLineSpace(int fontId, int lineSpace)
{
	if (m_fonts[fontId] == null)
		StringManager::LoadFont(fontId);

	m_fonts[fontId]->SetLineSpacing(lineSpace);
}

int StringManager::GetFontLineSpace(int fontId)
{
	if (m_fonts[fontId] == null)
		StringManager::LoadFont(fontId);
	return m_fonts[fontId]->GetLineSpacing();
}

void StringManager::DrawString(int fontId, int stringId, int x, int y, int anchor, bool Upchar , int color, int alpha,int rotate, int x_scale, int y_scale, bool WB, bool WH, int rotateAngle)
{
	if (m_fonts[fontId] == null)
		StringManager::LoadFont(fontId);//return;

	if((anchor & ASprite::TOP) == 0 && (anchor & ASprite::BOTTOM) == 0)
	{
		if((anchor & ASprite::VCENTER) == 0)
		{
			if((fontId == FONT_BLACK || fontId == FONT_SHINE || fontId == FONT_BLACK_SMALL || fontId == FONT_BLACK_SMALL_SHINE))
			{
				//y += 5;
				//anchor |= ASprite::VCENTER;
			}
			else if(fontId == FONT_BROWN_GAME || fontId == FONT_GREEN_GAME || fontId == FONT_WHITE_GAME || fontId == FONT_LOAD)
			{
				y += 12;
				anchor |= ASprite::VCENTER;
			}
		}
	}
	m_fonts[fontId]->DrawString(GetString(stringId), x, y, anchor, color, alpha, rotate, x_scale, y_scale, WB, WH, rotateAngle);	
}

bool StringManager::IsAsianLanguage()
{
	if(m_nLanguage == LANGUAGE_KR || m_nLanguage == LANGUAGE_SR || m_nLanguage == LANGUAGE_JP)
		return true;
	else
		return false;
}

//void StringManager::mywcsup(wchar_t* dest,size_t len)
//{
//	for(int i = 0;i< len;i++)
//	{
//		if(dest[i] >= L'a' || dest[i] >= L'z')
//		{
//			dest[i] += L'A' - L'a'
//		}
//	}
//}

void StringManager::DrawString(int fontId, TChar* s, int x, int y, int anchor, bool Upchar, int color, int alpha,int rotate, int x_scale, int y_scale, bool WB, bool WH, int rotateAngle)
{
	if (m_fonts[fontId] == null)
		StringManager::LoadFont(fontId);//return;
	//if(Upchar)
	//{
	//	CommonString::mywcsup(s,wcslen(s) + 1);
	//	m_fonts[fontId]->DrawString(s, x, y, anchor, color, alpha, rotate, x_scale, y_scale, WB, WH);	
	//}
	m_fonts[fontId]->DrawString(s, x, y, anchor, color, alpha, rotate, x_scale, y_scale, WB, WH, rotateAngle);	
}

int StringManager::DrawPage(int fontId, int stringId, int x, int y, int anchor, int width, int color, bool WB, bool unwrap)
{
	if (m_fonts[fontId] == null)
		StringManager::LoadFont(fontId);//return;

	return m_fonts[fontId]->DrawPage(GetString(stringId), x, y, width, anchor, color, WB, unwrap);
}

int StringManager::DrawPage(int fontId, TChar* s, int x, int y, int start, int line_num, int character_num, int anchor, int width, int color, bool WB, bool bDrawString, bool unwrap)
{
	if (m_fonts[fontId] == null)
		StringManager::LoadFont(fontId);//return;

	return m_fonts[fontId]->DrawPage(s, x, y, width, start, line_num, character_num, anchor, 2, true, color, WB, bDrawString, unwrap);
}

void StringManager::UpdateStringSize(int fontId, TChar* s)
{
	if (m_fonts[fontId] == null)
		StringManager::LoadFont(fontId);//return;

	m_fonts[fontId]->UpdateStringSize(s);
}

void StringManager::UpdateStringSize(int fontId, int stringId)
{
	if (m_fonts[fontId] == null)
		StringManager::LoadFont(fontId);//return;

	m_fonts[fontId]->UpdateStringSize(GetString(stringId));
}

////////////////////////////////////////////////////////////////////////////////////////////////////

void StringManager::SetEscapedDigitValue(int value, int index)
{
	ASSERT(index >= 0);
	ASSERT(index < MAX_ESCAPE_DIGITS);
	m_digitValues[index] = value;
	m_nCurrentlyEscapeDigit = 0;	//if you call this it means you will draw a string with escaped digit, reset this...
}

TChar* StringManager::GetSpecialCharacterString(TChar c)
{
	//Global Special Character interpreting
	if ( ( c == L'd' ) || (c == L'D') )
	{
		ASSERT(m_nCurrentlyEscapeDigit >= 0);
		ASSERT(m_nCurrentlyEscapeDigit < MAX_ESCAPE_DIGITS);
		SetNumberString( m_digitValues[m_nCurrentlyEscapeDigit] );
		m_nCurrentlyEscapeDigit++;		
		return m_digitString;
	}

	return NULL;
}

void StringManager::SetNumberString(int value)
{	
	//TODO: support negative if needed.?
	ASSERT(value >= 0);

	//Make sure the last byte is the null character so it is interpreted as null-terminated string when drawed...
	m_digitString[ESCAPE_DIGITS_MAX_DIGITS] = L'\0';

	//special case.
	if(value == 0)
	{
		m_digitString[0] = L'0';
		m_digitString[1] = L'\0';
		return;
	}

	int numDigits = 0;
	int numCharacters = 0;
	while(value > 0)
	{
		if (m_nLanguage == LANGUAGE_SP || m_nLanguage == LANGUAGE_FR)
		{
			if(numDigits != 0 && numDigits % 3 == 0 && value >= 10)
				m_digitString[numCharacters++]	= ' ';
		}
		else
		{
			if(numDigits != 0 && numDigits % 3 == 0)
			{	
				if(m_nLanguage == LANGUAGE_EN) 
					m_digitString[numCharacters++]	= L',';
				//else if (m_nLanguage == LANGUAGE_PT)
				//	m_digitString[numCharacters++]	= ' ';
				else
					m_digitString[numCharacters++]	= L'.';
			}
		}
		
		m_digitString[numCharacters++]	= ('0' + (value % 10));
		numDigits++;
		value = value / 10;
	}

	m_digitString[numCharacters] = L'\0';
	int middle = numCharacters / 2;
	TChar swapDigit;
	for(int i = 0; i < middle; i++)
	{
		numCharacters--;
		swapDigit = m_digitString[numCharacters];
		m_digitString[numCharacters] = m_digitString[i];
		m_digitString[i] = swapDigit;
		
	}	
}